package view;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import worldModel.LargeHeal;
import worldModel.MediumHeal;
import worldModel.Navigator;
import worldModel.SmallHeal;

public class Library {
	/**
	 * @author Chris Ray Created on 8:34:37 AM Nov 10, 2011
	 * @return
	 * @throws Exception
	 * 
	 */
	public static Navigator loadNavigator() throws Exception {
		Navigator returnNav = null;
		ObjectInputStream in = null;
		ObjectOutputStream out = null;

		File navDat;
		navDat = new File("navigator.dat");
		try {

			in = new ObjectInputStream(new FileInputStream(navDat));
			returnNav = (Navigator) in.readObject();
			in.close();
			System.out.println("load successful\n Navigator's location: "
					+ returnNav.getLocation().row + " "
					+ returnNav.getLocation().col);

		} catch (Exception e) {
			returnNav = Library.makeNavigator();
			saveNavigator(returnNav);
		}
		return returnNav;

	}

	public static void saveNavigator(Navigator n) {
		try {
			File navDat = new File("navigator.dat");
			ObjectOutputStream out = null;
			out = new ObjectOutputStream(new FileOutputStream(navDat));
			out.writeObject(n);
			out.flush();
			out.close();
			System.out.println("save successful");
		} catch (Exception e) {
			System.out.println("error saving");
		}
	}

	/**
	 * create a new navigator with 1 of each item in it's inventory.
	 * 
	 * @author Chris Ray Created on 4:34:26 AM Nov 10, 2011
	 * 
	 */
	protected static Navigator makeNavigator() {
		Navigator currentNav = new Navigator();
		currentNav.addItem(new LargeHeal());
		currentNav.addItem(new MediumHeal());
		currentNav.addItem(new SmallHeal());
		return currentNav;
	}
}
